﻿using RaceHFTools.BluetoothLE.Bean.Module;

namespace RaceHFTools.Utils
{
    public static class Url
    {
        public enum URL_CHANNEL
        {
            TEST,
            DEVELOP,
            RELEASE
        };

        private static string UrlBaseSite = @"https://manage.racehf.com/bean";

        public static string DeviceInfoUrl(byte[] id)
        {
            string sid = IdToString(id);
            string url = $"{UrlBaseSite}/device/info?id={sid}";
            return url;
        }

        public static string ProGetUrl(byte[] id)
        {
            string sid = IdToString(id);
            string url = $"{UrlBaseSite}/pro/get?id={sid}";
            return url;
        }

        public static string ProSetUrl(byte[] id, uint utc, uint pcode)
        {
            string sid = IdToString(id);
            string url = $"{UrlBaseSite}/pro/set?id={sid}&utc={utc}&pcode={pcode}";
            return url;
        }

        public static string GetOtaInfoUrl(DFU_TYPE type, URL_CHANNEL channel, byte[] id)
        {
            string stype = TypeToString(type);
            string schannel = ChannelToString(channel);
            string sid = IdToString(id);
            string url = $"{UrlBaseSite}/{stype}/{schannel}/info?id={sid}";
            return url;
        }

        public static string GetOtaBinUrl(DFU_TYPE type, URL_CHANNEL channel, byte[] id)
        {
            string stype = TypeToString(type);
            string schannel = ChannelToString(channel);
            string sid = IdToString(id);
            string url = $"{UrlBaseSite}/{stype}/{schannel}/ota.bin?id={sid}";
            //string url = $"{UrlBaseSite}/{stype}/{schannel}/ota.bin?id=000000600000000010c046d840200538";
            return url;
        }

        static string ChannelToString(URL_CHANNEL channel)
        {
            string schannel = string.Empty;

            switch (channel)
            {
                case URL_CHANNEL.TEST:
                    schannel = "test";
                    break;

                case URL_CHANNEL.DEVELOP:
                    schannel = "develop";
                    break;

                case URL_CHANNEL.RELEASE:
                    schannel = "release";
                    break;
            }

            return schannel;
        }

        static string TypeToString(DFU_TYPE type)
        {
            string stype = string.Empty;

            switch (type)
            {
                case DFU_TYPE.BOOTLOADER:
                    stype = "bootloader";
                    break;

                case DFU_TYPE.FIRMWARE:
                    stype = "firmware";
                    break;
            }

            return stype;
        }

        static string IdToString(byte[] id)
        {
            string sid = string.Empty;

            foreach (byte x in id)
                sid += x.ToString("x2");

            return sid;
        }
    }
}
